package controllers;

import java.io.File;
import play.*;
import play.mvc.*;
import play.cache.Cache;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import models.*;

public class UserInfoSet extends Application {

    @Before
    private static void setup() {
        renderArgs.put("usr", getUser());
    }

    public static void index() {
        render("UserInfoSet/index.html");
    }

    public static void setUserInfo(String name, String password, String repassword) {
        User usr = getUser();
        if (!password.equals(repassword)) {
            flash.error("两次输入密码不相同");
            index();
        }
        if (name.isEmpty()) {
            flash.error("用户名不能为空");
            index();
        }
        if (!usr.name.equals(name)) {
            long usrCnt = User.count("name = ? ", name);
            if (usrCnt != 0) {
                flash.error("用户名已存在");
                index();
            }
        }
        usr.name = name;
        usr.password = password;
        usr.save();
        index();
    }

    public static void photo() {
        render();
    }
    
    public static void background() {
        render();
    }

    public static void savePhoto(File attachment) {
        System.out.println(attachment.toString());
        System.out.println(attachment.getName());
        try {
            User usr = getUser();
            String filePath = "public/images/userImgFolder";
            File myFilePath = new File(filePath);
            if (!myFilePath.exists()) {//创建头像图片目录
                myFilePath.mkdir();
                System.out.println("************" + myFilePath.getAbsolutePath() + "\n\n");
            }
            //String dest = myFilePath.getAbsolutePath() + "/" + usr.name+attachment.toString().substring(attachment.toString().lastIndexOf("."), attachment.toString().length());//以用户名作为头像图片名
            String dest = myFilePath.getAbsolutePath() + "/" + usr.name + attachment.toString().substring(attachment.toString().lastIndexOf("."), attachment.toString().length());
            FileInputStream in = new FileInputStream(attachment.getPath());//保存头像图片
            File file = new File(dest);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(file);
            int c;
            byte buffer[] = new byte[1024];
            while ((c = in.read(buffer)) != -1) {
                for (int i = 0; i < c; i++) {
                    out.write(buffer[i]);
                }
            }
            in.close();
            out.close();

            usr.avatar = "/public/images/userImgFolder/" + file.getName();
            System.out.println(file.getCanonicalPath());
            usr.save();
        } catch (Exception e) {
            System.out.println("file error ***************************");
            e.printStackTrace();
        }
        photo();
    }
    
    //保存用户背景图片
    public static void saveBackground(File attachment) {
    	 try {
            User usr = getUser();
            String filePath = "public/images/userBackground";
            File myFilePath = new File(filePath);
            if (!myFilePath.exists()) {//创建背景图片目录
                myFilePath.mkdir();
                System.out.println("************" + myFilePath.getAbsolutePath() + "\n\n");
            }
            String dest = myFilePath.getAbsolutePath() + "/" + usr.name + attachment.toString().substring(attachment.toString().lastIndexOf("."), attachment.toString().length());
            FileInputStream in = new FileInputStream(attachment.getPath());//保存背景图片
            File file = new File(dest);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(file);
            int c;
            byte buffer[] = new byte[1024];
            while ((c = in.read(buffer)) != -1) {
                for (int i = 0; i < c; i++) {
                    out.write(buffer[i]);
                }
            }
            in.close();
            out.close();

            usr.background = "/public/images/userBackground/" + file.getName();
            System.out.println(file.getCanonicalPath());
            usr.save();
        } catch (Exception e) {
            System.out.println("file error ***************************");
            e.printStackTrace();
        }
        background();
    }
    
}
